//
//  MTProgressView.swift
//  MT
//
//  Created by M on 2020/1/1.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTProgressView: UIView {
    
    private(set) public var progressBarWidth: CGFloat
    private(set) public var progressBarBorder: CGFloat
    private(set) public var progressBarRadius: CGFloat
    
    /// 完成闭包
    var completionHandler: (()->())?
    
    var text: String {
        didSet {
            self.progressLabel.text = text
        }
    }
    
    public var progress: CGFloat = 0.0 {
        didSet {
            if progress >= 1 && !completed {
                if let completionHandler = completionHandler {
                    completionHandler()
                }
            }
            self.setNeedsDisplay()
        }
    }
    
    var isBGLayerHidden: Bool = false
    
    private var progressBGLayer: CAShapeLayer!
    private var progressLayer: CAShapeLayer!
    private var completed: Bool = false
    
    init(frame: CGRect, text: String, progressBarWidth: CGFloat = 20.0, borderWidth: CGFloat = 2.0, completionHandler: (() -> Swift.Void)? = nil) {
        self.progressBarWidth = progressBarWidth
        self.progressBarBorder = borderWidth
        self.progressBarRadius = progressBarWidth / 2 - borderWidth
        self.text = text
        
        super.init(frame: frame)
        self.addSubview(progressLabel)
        
        progressBGLayer = CAShapeLayer()
        progressBGLayer.frame = CGRect(x: (self.frame.width - progressBarWidth) / 2, y: progressLabel.frame.origin.y - progressBarWidth - 10, width: progressBarWidth, height: progressBarWidth)
        progressBGLayer.fillColor = UIColor.clear.cgColor
        progressBGLayer.strokeColor = UIColor.white.withAlphaComponent(0.1).cgColor
        progressBGLayer.opacity = 1
        progressBGLayer.lineCap = .round
        progressBGLayer.lineWidth = progressBarBorder
        self.layer.addSublayer(progressBGLayer)
        
        progressLayer = CAShapeLayer()
        progressLayer.frame = CGRect(x: (self.frame.width - progressBarWidth) / 2, y: progressLabel.frame.origin.y - progressBarWidth - 10, width: progressBarWidth, height: progressBarWidth)
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = UIColor.white.cgColor
        progressLayer.opacity = 1
        progressLayer.lineCap = .round
        progressLayer.lineWidth = progressBarBorder
        self.layer.addSublayer(progressLayer)
    }
    
    lazy var progressLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: self.frame.height / 2 + progressBarWidth / 2 + 12, width: self.frame.width, height: 12))
        label.text = self.text
        label.textColor = UIColor.white
        label.font = PingFangSCMediumFont(12)
        label.textAlignment = .center
        return label
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MTProgressView {
    
    override func draw(_ rect: CGRect) {
        drawProgress()
    }
    
    private func drawProgress() {
        let center: CGPoint = CGPoint(x: progressBarWidth/2, y: progressBarWidth/2)
        let startAngle: CGFloat = -CGFloat.pi/2
        let endAngle: CGFloat = -CGFloat.pi/2 + CGFloat.pi * 2 * progress
        
        let bgPathEndAngle: CGFloat = -CGFloat.pi/2 + CGFloat.pi * 2
        let bgPath = UIBezierPath(arcCenter: center, radius: progressBarRadius, startAngle: startAngle, endAngle: bgPathEndAngle, clockwise: true)
        progressBGLayer.path = bgPath.cgPath
        
        let path = UIBezierPath(arcCenter: center, radius: progressBarRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        progressLayer.path = path.cgPath
    }
    
}

